package com.laboratory.util.json;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import com.laboratory.util.StringUtil;
import net.sf.json.JSONArray;
import net.sf.json.JSONException;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;

/**
 * Created by 30868 on 2019/05/13.
 */
public class JSONUtil {
    public static final  org.apache.log4j.Logger  logger = org.apache.log4j.Logger.getLogger(JSONUtil.class);
    private static final String JSONOBJECT = "JSONOBJECT";	//对象
    private static final String JSONARRAY = "JSONARRAY";	//集合
    private static final String NORMAL = "NORMAL";			//普通

    /**
     * 根据返回值转换成map集合
     * @param json json字符串,格式如 {"id":1001, "name":"张三"}
     * @return map集合
     */
    public static Map<String, Object> jsonToMap(String json) {
        Map<String, Object> map = null;
        try {
            map = new HashMap<String, Object>();
            JSONObject jsonObject = JSONObject.fromObject(json);
            Iterator<String> keyIterator = jsonObject.keys();
            while (keyIterator.hasNext()) {
                String key = (String) keyIterator.next();
                Object value = jsonObject.get(key);
                map.put(key, value == null || JSONNull.getInstance().equals(value)?null:value);
            }
        } catch (JSONException e) {
            logger.error("jsonToMap", e);
            return null;
        }
        return map;
    }
    /**
     * 根据返回值转换成list集合
     * @param json json字符串,格式如[{}, {}]
     * @return list集合
     */
    public static List<Map<String, Object>> jsonToListMap(String json) {
        List<Map<String, Object>> list = null;
        try {
            list = new ArrayList<Map<String,Object>>();
            JSONArray array = JSONArray.fromObject(json);
            for (int i = 0; i < array.size(); i++) {
                JSONObject jsonObject = array.getJSONObject(i);
                list.add(jsonToMap(jsonObject.toString()));
            }
        } catch (JSONException e) {
            logger.error("jsonToListMap", e);
            return null;
        }
        return list;
    }

    public static String getJsonTypeBak(String jsonStr) {
        String type = NORMAL;
        if (!StringUtils.isEmpty(jsonStr)) {
            try {
                JSONObject.fromObject(jsonStr);
                type = JSONOBJECT;
            } catch (JSONException e) {
                logger.error("getJsonType", e);
                try {
                    JSONArray.fromObject(jsonStr);
                    type = JSONARRAY;
                } catch (JSONException e1) {
                    logger.error("getJsonType", e1);
                    type = NORMAL;
                }

            }
        }
        return type;
    }
    public static String getJsonType(String jsonStr) {
        String type = NORMAL;
        if (!StringUtil.isEmpty(jsonStr)) {
            if(jsonStr.startsWith("{")){
                type = JSONOBJECT;
            }else if (jsonStr.startsWith("[")){
                type = JSONARRAY;
            }else {
                type = NORMAL;
            }
        }
        return type;
    }
    public static void main(String[] args) {
    }

}
